package com.shengwei.truck.driver.view.ui.task;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.tabs.TabLayout;
import com.shengwei.truck.driver.R;
import com.shengwei.truck.driver.databinding.FragmentTaskBinding;
import com.shengwei.truck.driver.utils.MyLogCat;
import com.shengwei.truck.driver.view.adapter.task.TaskFragmentPagerAdapter;
import com.shengwei.truck.driver.view.ui.BaseFragment;
import com.shengwei.truck.driver.view.ui.MainActivity;
import com.shengwei.truck.driver.view.ui.home.HomeFragment;
import com.shengwei.truck.driver.vm.TaskViewModel;

/**
 * 任务列表：展示派发给司机的任务单列表，顶端是一个TAB标签页，可以切换查看不同状态下的任务单列表
 */
public class TaskFragment extends BaseFragment<TaskViewModel, FragmentTaskBinding> {
    String TAG  = "TaskFragment";
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        MyLogCat.i(TAG,"onCreateView");
        View rootView =  super.onCreateView(inflater,container,savedInstanceState);
        //初始化控件
        initView();
        return rootView;
    }
    @Override
    public void onStart() {
        MyLogCat.i(TAG,"onStart");
        if(!HomeFragment.isTaskListEmpty && MainActivity.mCurTabIndex == 1){
            mDataBinding.viewPager.setCurrentItem(1);
        }
        super.onStart();
    }
    @Override
    public void onResume() {
        MyLogCat.i(TAG,"onResume");
        super.onResume();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden){ //显示
            if(!HomeFragment.isTaskListEmpty){
                mDataBinding.viewPager.setCurrentItem(1);
            }
            else{
                mDataBinding.viewPager.setCurrentItem(0);
            }
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_task;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    /**
     * 初始化控件
     */
    public void initView() {
        String title = mContext.getResources().getString(R.string.bottom_tab_task);
        setToolBar(false,true,title);
        //tablayout+viewpager+fragment实现可滑动的任务列表TAB页
        TaskFragmentPagerAdapter taskFragmentPagerAdapter = new TaskFragmentPagerAdapter(mContext,getChildFragmentManager());
        mDataBinding.viewPager.setAdapter(taskFragmentPagerAdapter);
        mDataBinding.tablayout.setupWithViewPager(mDataBinding.viewPager);
        //defineTabTitle(mDataBinding.tablayout); //自定义Tab标签的布局
        mDataBinding.viewPager.setCurrentItem(0);
    }

    /**
     * 自定义TabLayout的Tab标签的布局和样式
     */
    public void defineTabTitle(TabLayout tabLayout){
        for (int tabIndex = 0; tabIndex < tabLayout.getTabCount(); tabIndex++) {
            TabLayout.Tab tab = tabLayout.getTabAt(tabIndex);
            View view = View.inflate(getActivity(), R.layout.bottom_navigation, null);
            TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
            switch (tabIndex){
                case 0:
                    tv_name.setText(R.string.new_order);
                    break;
                case 1:
                    tv_name.setText(R.string.new_task);
                    break;
                case 2:
                    tv_name.setText(R.string.has_complete);
                    break;
                case 3:
                    tv_name.setText(R.string.has_forward);
                    break;
            }
            tab.setCustomView(view);
            //tab.setIcon(icon)
        }
    }

}
